/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Reflection;

namespace OpenServers.Net.Common.Utils {
    /// <summary>
    /// collection of functions used to simplifiy reflection.
    /// </summary>
    public class ReflectionUtils {
        /// <summary>
        /// Gets the types from an array of objects.
        /// </summary>
        public static Type[] GetTypes(object[] parameters) {
            Type[] types = new Type[parameters.Length];
            for (int i = 0; i < parameters.Length; i++) {
                if (parameters[i] == null) {
                    throw new ArgumentNullException("parameters", "a member of parameters cannot be null.");
                }
                types[i] = parameters[i].GetType();
            }
            return types;
        }

        /// <summary>
        /// Gets a method.
        /// </summary>
        public static MethodInfo GetMethod(Type type, string methodName, Type[] types) {
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            MethodInfo result = type.GetMethod(methodName, bindingFlags, null, types, null);
            if (result == null && type.BaseType != null) {
                result = GetMethod(type.BaseType, methodName, types);
            }
            return result;
        }

        /// <summary>
        /// Gets all the methods with a given name.
        /// </summary>
        public static MethodInfo[] GetMethods(Type type, string methodName) {
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            MethodInfo[] methods = type.GetMethods(bindingFlags);
            List<MethodInfo> results = new List<MethodInfo>();
            foreach (MethodInfo methodInfo in methods) {
                if (methodInfo.Name == methodName) {
                    results.Add(methodInfo);
                }
            }
            return results.ToArray();
        }

        /// <summary>
        /// Gets all the methods with a given name and parameter count.
        /// </summary>
        public static MethodInfo[] GetMethods(Type type, string methodName, int parameterCount) {
            MethodInfo[] methods = GetMethods(type, methodName);
            List<MethodInfo> results = new List<MethodInfo>();
            foreach (MethodInfo methodInfo in methods) {
                if (methodInfo.GetParameters().Length == parameterCount) {
                    results.Add(methodInfo);
                }
            }
            return results.ToArray();
        }

        /// <summary>
        /// Gets a static field.
        /// </summary>
        public static FieldInfo GetStaticField(Type type, string fieldName) {
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
            FieldInfo result = type.GetField(fieldName, bindingFlags);
            if (result == null && type.BaseType != null) {
                result = GetStaticField(type.BaseType, fieldName);
            }
            return result;
        }
    }
}
